// 人脸采集
function faceInfoCollection(that) {
    let _this = that;
    return new Promise((resolve, reject) => {
        const plus = _this.$store.state.config.plus ? _this.$store.state.config.plus : window.plus;
        const camera = _this.$store.state.config.plus ? _this.$store.state.config.plus.camera.getCamera(2) : window.plus.camera.getCamera(2);
        camera.captureImage(function(path) {
            // path  为获取拍照照片的url
            plus.io.resolveLocalFileSystemURL(path, async(entry) => {
                // entry 为获取的文件对象
                // 图片太大，进行压缩
                const faceSrc = entry.toLocalURL();
                const res = await compressImage(
                    entry.toLocalURL(),
                    entry.name,
                    _this
                );
                // 将压缩后的转化为base64格式
                const url = await showPics(res.target, entry.name, _this);
                // return url;
                let data = {
                    urlBase: url,
                    pathSrc: faceSrc
                };
                resolve(data);
            });
        }, function(e) { // 拍照错误
        });
    });
}
// 压缩图片
function compressImage(url, filename, _this) {
    const plus = _this.$store.state.config.plus ? _this.$store.state.config.plus : window.plus;
    return new Promise((resolve, reject) => {
        var name = '_doc/upload/' + filename;
        plus.zip.compressImage(
            {
                src: url, //src: (String 类型 )压缩转换原始图片的路径
                dst: name, //压缩转换目标图片的路径
                quality: 90, //quality: (Number 类型 )压缩图片的质量.取值范围为1-100
                overwrite: true, //overwrite: (Boolean 类型 )覆盖生成新文件
                width: '250',
                height: '320'
            },
            function(zip) {
                //页面显示图片
                // showPics(zip.target, name);
                console.log(zip, 'zip');
                resolve(zip);
            },
            function(error) {
                console.log(error, 'error');
                reject(error);
                // plus.nativeUI.toast("压缩图片失败，请稍候再试");
            }
        );
    });
}
// 转base64
function showPics(url, name, that) {
    let _this = that;
    const plus = _this.$store.state.config.plus ? _this.$store.state.config.plus : window.plus;
    return new Promise((resolve, reject) => {
        //根据路径读取到文件
        plus.io.resolveLocalFileSystemURL(url, function(entry) {
            entry.file(function(file) {
                var fileReader = new plus.io.FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onloadend = function(e) {
                    var picUrl = e.target.result.toString();
                    resolve(picUrl);
                };
            });
        });
    });
}
export default faceInfoCollection;
